package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.common.util.PageResult;
import com.qk.domain.business.BusinessDo;
import com.qk.domain.business.BusinessFollwUp;
import com.qk.domain.business.BusinessPoolDto;
import com.qk.dto.clue.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.entity.User;
import com.qk.management.constant.BusinessStatusConstants;
import com.qk.management.constant.ClueStatusConstants;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void addBusiness(BusinessDto bdo) {
        boolean b = BeanUtil.hasNullField(bdo, "courseId", "name", "gender", "age", "wechat", "qq", "subject", "degree", "jobStatus", "remark");
        if (b) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Business business = BeanUtil.copyProperties(bdo, Business.class);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(ClueStatusConstants.WAIT_ALLOCATION);
        this.baseMapper.insert(business);

    }

    @Override
    public PageResult<BusinessVo> getBusiness(BusinessDo bd) {
        PageHelper.startPage(bd.getPage(), bd.getPageSize());
        Page<BusinessVo> rs = this.baseMapper.getBusiness(bd);
        return PageResult.<BusinessVo>builder()
                .total((int) rs.getTotal())
                .rows(rs)
                .build();

    }

    @Override
    public BusinessVo getBusinessById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        Integer userId = business.getUserId();
        User user = userMapper.selectById(userId);
        List<BusinessTrackRecord> businessTrackRecords = businessTrackRecordMapper.selectList(Wrappers.lambdaQuery(BusinessTrackRecord.class).eq(BusinessTrackRecord::getBusinessId, id));
        BusinessVo businessVo = BeanUtil.copyProperties(business, BusinessVo.class);
        List<BusinessVo.BusinessTrackRecordVo> list = businessTrackRecords.stream().map(trackRecord -> {
            BusinessVo.BusinessTrackRecordVo businessTrackRecordVo = BeanUtil.copyProperties(trackRecord, BusinessVo.BusinessTrackRecordVo.class);
            businessTrackRecordVo.setAssignName(user.getName());
            return businessTrackRecordVo;
        }).toList();
        businessVo.setTrackRecords(list);
        return businessVo;
    }

    @Override

    public void updateById(BusinessFollwUp bfu) {
        Business business = BeanUtil.copyProperties(bfu, Business.class);
        business.setUpdateTime(LocalDateTime.now());
        if (bfu.getStatus() == BusinessStatusConstants.WAIT_FOLLOW_UP) {
            business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        }
        this.baseMapper.updateById(business);
        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(bfu, BusinessTrackRecord.class);
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setId(null);
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserContextHolders.get());
//        businessTrackRecord.setTrackStatus(BusinessTrackStatus.NORMAL);
        businessTrackRecordMapper.insert(businessTrackRecord);

    }

    @Override
    public void toCustomerById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.CONVERT_TO_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
        Customer customer = BeanUtil.copyProperties(business, Customer.class);
        customer.setId(null);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setBusinessId(id);
        this.customerMapper.insert(customer);
    }

    @Override
    public PageResult<BusinessVo> pool(BusinessPoolDto b) {
        PageHelper.startPage(b.getPage(), b.getPageSize());
        Page<BusinessVo> rs = this.baseMapper.pool(b);
        return PageResult.<BusinessVo>builder()
                .total((int) rs.getTotal())
                .rows(rs)
                .build();
    }


}
